package Second;

import java.util.Scanner;

/**
 * @author 长风无言
 */
public class Compare {

/**
 * description:
 *
 * @param s1 字符串1
 * @param s2 字符串2
 * @return java.lang.String
 */
    public static String getMaxSametring(String s1, String s2) {
        String max = "", min = "";
        max = (s1.length() > s2.length()) ? s1 : s2;
        min = (max == s1) ? s2 : s1;

        for (int i = 0; i < min.length(); i++) {
            //较短字符串从最大长度依次递减的子串与较长的串比较
            for(int j=0,k=min.length()-i;k!=min.length()+1;j++,k++) {
                String temp = min.substring(j,k);
                //判断是否包含其中
                if (max.contains(temp)) {
                    return temp;
                }
            }
        }
        return "";
    }

/**
 * description: 进行字符串比较
 *
 * @param args 语句
 * @return void
 */
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入String1:");
        String string1 = scanner.next();
        System.out.println("请输入String2:");
        String string2 = scanner.next();
//        String string1 = "hello", string2 = "1h8whellos9i3";
        String flag = getMaxSametring(string1, string2);
        System.out.println("最大相同字符串为："+flag);
    }
}
